<?php
	if(getRequestParameter("add_coupon") !== false)
	{
		$messages = array();
		$coupon = getRequestParameter("add_coupon");
		$client = getLoginData();
		
		
		if(isset($_SESSION["coupons"][$coupon]))
		{
			$messages["error"] = translate("coupon_already_added");
		}
		elseif(isset($client["id"]))
		{
			if($coupon != "")
			{
				$discount = getCouponDiscount($coupon);
				if(!isset($discount["id"]))
				{
					$messages["error"] = translate("invalid_coupon");
				}
				else
				{
					if($discount["start"] <= time() && time() <= $discount["end"])
					{
						$error = false;
						if($discount["product_id"] > 0)
						{
							$productID = $discount["product_id"];
							if(!isset($_SESSION["cart"][$productID]) || count($_SESSION["cart"][$productID]) == 0)
							{
								$error = true;
								
								$product = getProductByField($productID, "id");
								
								$messages["error"] = translate("coupon_error_add_product_to_cart");
								$messages["link"] = " <br /> <strong><a href='{$product["url"]}' title='{$product["name"]}'>{$product["name"]}</a><strong>";
								
							}
						}
						elseif($discount["category_id"] > 0)
						{
							$categoryID = $discount["category_id"];
							
							// TODO: verify if there are products matching this category

						}
						
						if($error == false)
						{
							$_SESSION["coupons"][$coupon] = $coupon;
							
							$messages["success"] = translate("coupon_added");
						}
					}
					else
					{
						$messages["error"] = translate("discount_expired");
					}
				}
			}
			else
			{
				$messages["error"] = translate("invalid_coupon");
			}
		}
		else
		{
			$loginPage = get_site_page_by_slug("login", $_SESSION["language"]["id"]);
			
			$messages["error"] = translate("coupons_need_to_login");
			$messages["link"] = "<a href='".site_url().$loginPage["url"].".html'>" . translate("log_in_now") . "</a>";
		}
		
		echo json_encode($messages);
	}

	if(getRequestParameter("remove_from_cart") !== false)
	{
		$messages = array();
		
		if(getRequestParameter("product_id") !== false && getRequestParameter("stock_id") !== false)
		{
			$productID = getRequestParameter("product_id");
			$stockID = getRequestParameter("stock_id");
			
			if(isset($_SESSION["cart"][$productID][$stockID]))
			{
				$product = getProductByField($stockID, "stock_id");
				if(!isset($product["id"]))
				{
					unset($_SESSION["cart"][$productID][$stockID]);
					
					$messages["error"] = translate("unknown_error");
					$messages["remove_row"] = $stockID;
				}
				else
				{
					unset($_SESSION["cart"][$productID][$stockID]);
					$currency = get_setting("currency");
					
					$grandTotal = 0;
					$perTotal = 0;
					
					$transportPrice = get_setting("transport_price");
					$products = getCartProducts();
					
					
					
					if(count($products) > 0)
					{
						$transportPrice = $products[0]["transport_price"];
						$grandTotal = $products[0]["grand_total"];
						$perTotal = $products[0]["products_price"];
						
					}
					$messages["new_per_total"] = makePriceInterval($perTotal, 0, $currency);
					$messages["new_grand_total"] = makePriceInterval($grandTotal, 0, $currency);
					$messages["transport_price"] = makePriceInterval($transportPrice, 0, $currency);
					$messages["remove_row"] = $stockID;
					$messages["success"] = translate("add_to_cart_removed_product");
				}
				
			}
			else
			{
				$messages["error"] = translate("unknown_error");
			}
		}
		else
		{
			$messages["error"] = translate("unknown_error");
		}
		
		echo json_encode($messages);
	}
	
	if(getRequestParameter("change_quantity") !== false)
	{
		$messages = array();
		
		if(getRequestParameter("product_id") !== false && getRequestParameter("stock_id") !== false)
		{
			if(getRequestParameter("quantity") !== false && is_numeric(getRequestParameter("quantity")) && getRequestParameter("quantity") > 0)
			{
				$quantity = floor(getRequestParameter("quantity"));
				$productID = getRequestParameter("product_id");
				$stockID = getRequestParameter("stock_id");
				
				if(isset($_SESSION["cart"][$productID][$stockID]))
				{
					$product = getProductByField($stockID, "stock_id");
					
					if(!isset($product["id"]))
					{
						unset($_SESSION["cart"][$productID][$stockID]);
						
						$messages["error"] = translate("unknown_error");
						$messages["remove_row"] = $stockID;
					}
					else
					{
						if($product["quantity"] < $quantity)
						{
							 $messages["error"] = translate("add_to_cart_failed_no_stock");
						}
						else
						{
							$currency = get_setting("currency");
							$transportPrice = get_setting("transport_price");
							$vat = get_setting("vat");
							$products = getCartProducts();
							
							$_SESSION["cart"][$productID][$stockID] = $quantity;
							
							$newTotal = $product["min_price"] * $quantity;
							$newVat = ($newTotal * $vat)/100;
							
							$messages["new_total"] = makePriceInterval($newTotal, 0, $currency);
							$messages["new_vat"] = makePriceInterval($newVat,0,$currency);
							
							$grandTotal = 0;
							
							if(count($products) > 0)
							{
								
								if($products[0]["grand_total"] > $transportPrice)
								{
									$transportPrice = 0;
								}
								
								$grandTotal = $products[0]["grand_total"]+$transportPrice;
								
								
							}
							$messages["new_per_total"] = makePriceInterval($products[0]["grand_total"], 0, $currency);
							$messages["new_grand_total"] = makePriceInterval($grandTotal, 0, $currency);
							$messages["transport_price"] = makePriceInterval($transportPrice, 0, $currency);
							
							$messages["success"] = translate("add_to_cart_modified_quantity");
						}
					}
				}
				else
				{
					$messages["error"] = translate("unknown_error");
				}
			}
			else
			{
				$messages["error"] =  translate("add_to_cart_failed_no_quantity");
			}
		}
		else
		{
			$messages["error"] = translate("unknown_error");
		}
		
		echo json_encode($messages);
		
		return false;
	}
	
	if(getRequestParameter("refresh_cart") !== false)
	{
		getHeaderCart(true);
	} 
?>